const fs = require('node:fs');
const yaml = require('js-yaml');
const path = require('node:path');
const nodemailer = require('nodemailer');


const mail = fs.readFileSync(path.join(__dirname, '../email.yaml'), 'utf-8')
const mailData = yaml.load(mail)

const transporter = nodemailer.createTransport({
  host: 'smtp.163.com', // QQ邮箱的SMTP服务器地址
  port: 465, // 使用SSL时的端口
  secure: true, // 使用SSL
  auth: mailData
})

function sendMail(to, subject, text) {
  const mailOptions = {
    from: 'qaqrimo@163.com', // 发件人地址
    to: to, // 收件人地址
    subject: subject, // 邮件主题
    text: text // 邮件正文
  }
  transporter.sendMail(mailOptions, (err, info) => {
    if (err) {
      console.log('Error occurred:', err.message);
      return;
   }
   console.log('Message sent:', info.response);
  })
}

module.exports = { transporter, sendMail }
